﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using ActivateYourGlutes.Infrastructure.Logging;
using ActivateYourGlutes.Model.Contracts;
using ActivateYourGlutes.Model.Providers;
using ActivateYourGlutes.Tests.Unit.Mocks;
using Microsoft.Practices.ServiceLocation;
using NUnit.Framework;
using Rhino.Mocks;

namespace ActivateYourGlutes.Tests.Unit.Controllers
{
    public class ControllerTestBase
    {
        protected UnitOfLength _centimeters;
        protected UnitOfWeight _kilogrammes;
        protected Account _mockAccount;
        protected UserLocaleSettings _localeSettings;
        protected List<MeasurementType> _measurementTypes;
        protected MeasurementType _weight;
        protected MeasurementType _bodyFat;
        protected MeasurementType _chest;
        protected IAccountProvider _commonAccountProvider;
        protected MockHttpContext _httpContext;
        protected ILogger _logger;

        [SetUp]
        public virtual void Init()
        {
            _weight = new MeasurementType() {MeasurementTypeId = 1, Description = "Weight", Type = "W" };
            _bodyFat = new MeasurementType() {MeasurementTypeId = 2, Description = "Body Fat %", Type = "P"};
            _chest = new MeasurementType() {MeasurementTypeId = 3, Description = "Chest", Type = "L"};
            _centimeters = new UnitOfLength() { Description = "cm", Unit = "cm", ToMetersMultiplier = 0.01};
            _kilogrammes = new UnitOfWeight() { Description = "kg", Unit = "kg", ToKgMultiplier = 1};
            _mockAccount = new Account()
            {
                AccountId = 1,
                DateOfBirth = new DateTime(1990, 1, 1),
                LocalDateFormat = "dd-MM-yyyy",
                PrimaryMeasurementType = _weight,
                SecondaryMeasurementType = _bodyFat,
                UnitOfLength = _centimeters,
                UnitOfWeight = _kilogrammes,
                IsAdministrator = false
            };
            _localeSettings = new UserLocaleSettings()
            {
                DateFormat = "dd-MM-yyyy",
                UnitOfLength = _centimeters,
                UnitOfWeight = _kilogrammes
            };
            _measurementTypes = new List<MeasurementType>() {_weight, _bodyFat, _chest};
            _logger = MockRepository.GenerateStub<ILogger>();
            IServiceLocator locator = MockRepository.GenerateStub<IServiceLocator>();
            ILocalisationProvider localisationProvider = MockRepository.GenerateStub<ILocalisationProvider>();
            localisationProvider.Stub(lp => lp.UnitOfLength).Return(_localeSettings.UnitOfLength);
            localisationProvider.Stub(lp => lp.UnitOfWeight).Return(_localeSettings.UnitOfWeight);
            locator.Stub(l => l.GetInstance<ILocalisationProvider>()).Return(localisationProvider);
            ServiceLocator.SetLocatorProvider(() => locator);

            _commonAccountProvider = MockRepository.GenerateStub<IAccountProvider>();
            _commonAccountProvider.Stub(cap => cap.CurrentAccount(null)).IgnoreArguments().Return(_mockAccount);

            _httpContext = new MockHttpContext();
        }

        [TearDown]
        public virtual void TearDown()
        {
            _centimeters = null;
            _kilogrammes = null;
            _mockAccount = null;
            _localeSettings = null;
        }

        /// <summary>
        /// Performs a standard check for redirection to the unauthorized page
        /// </summary>
        protected void AssertRedirectToUnauthorized(RedirectToRouteResult result)
        {
            Assert.That(result.RouteValues["controller"], Is.EqualTo("Account"));
            Assert.That(result.RouteValues["action"], Is.EqualTo("Unauthorized"));
        }
    }
}
